﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using SportStore.DomainModel.Abstract;

namespace SportStore.WebUI.Controllers
{
    public class NavController : Controller
    {
        private IProductsRepository productRepository;
        public NavController(IProductsRepository productRepository)
        {
            this.productRepository = productRepository;
        }

        public ViewResult Menu(string highlightCateogry)
        {
            List<NavLink>navLink =  new List<NavLink>();
            navLink.Add(new CategoryLink(null) { IsSelected = (highlightCateogry==null) });

            var categories = productRepository.Products.Select(x => x.Category);
            foreach (string category in categories.Distinct().OrderBy(x=>x))
            {
                navLink.Add(new CategoryLink(category) { IsSelected = (category == highlightCateogry)});
            }
            return View(navLink);
        }

        public ActionResult Index()
        {
            return View();
        }

    }

    public  class NavLink
    {
        public string Text { get; set; }
        public RouteValueDictionary RouteValues { get; set; }
        public bool IsSelected { get; set; }
    }

    public  class CategoryLink: NavLink
    {
        public  CategoryLink(string category)
        {
            Text = category ?? "Home";
            RouteValues = new RouteValueDictionary(new {Controller = "Products", action = "List", category = category, page = 1});
        }
    }
}
